﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BigClipboard.Modules.ClipboardDataModule;
using Azylee.Core.IOUtils.TxtUtils;
using JustMetClient.Commons;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using JustMetClient.Tools.FileUtil;
using JustMetClient.Tools.TbUtil;

namespace JustMetClient.ViewControls
{
    public partial class OtherFunPart : UserControl
    {
        //创建委托
        public delegate void MyDelegate();
        public object _lockThread = new object();
        public static MyDelegate _mydelegate;
        public String url_result = "";

        public OtherFunPart()
        {
            InitializeComponent();
            //初始化委托
            _mydelegate = new MyDelegate(RefreshView);
            Tb_net1.Text = "http://58.87.76.162:8213/study/api/getTokenByJstAccount?account=15192761251&type=ycyc";
        }


        //1.创建文本
        private void Btn_CreateTxt_Click(object sender, EventArgs e)
        {
            CreateAndOpenTxt();
        }



        //2.打开位置
        private void Btn_Open_CreateTxt_Click(object sender, EventArgs e)
        {
            string fileName = R.Paths.MyDataPath + "\\create_txt\\";
            System.Diagnostics.Process.Start(fileName);
        }

        //2.文件复制
        private void Btn_File_Copy_Click(object sender, EventArgs e)
        {
            if (TextBoxTools.CheckTextBox(Tb_Copy_Count))
            {
                string newFilePath = R.Paths.MyDataPath + "\\file_copy\\" + Guid.NewGuid().ToString() + "\\";
                //源文件路径
                string filePath = FileTool.SelectFilePath();
                if (string.IsNullOrEmpty(filePath))
                {
                    MessageBox.Show("未选择文件！");
                    return;
                }

                int copyCount = Int32.Parse(Tb_Copy_Count.Text);
                Task.Factory.StartNew(() =>
                {
                    for(int i = 0; i < copyCount; i++)
                    {
                        FileCopy(filePath, newFilePath);
                    }
                    System.Diagnostics.Process.Start(newFilePath);
                });
            }
        }

        //3.模拟Httpget请求
        private void Btn_ok_Click(object sender, EventArgs e)
        {
            Start();
        }


        #region 文件复制
        private void FileCopy(string filePath, string targetDir)
        {
            string newFileName = Guid.NewGuid().ToString();
            FileTool.FileCopy(filePath, newFileName, targetDir);
        }
        #endregion

        #region 保存操作
        /// <summary>
        /// 文本保存到本地
        /// </summary>
        /// <param name="img"></param>
        public static void CreateAndOpenTxt()
        {
            string fileName = R.Paths.MyDataPath + "\\create_txt\\" + ClipboardDataMan.GetTimeNoToDay()+ "-" +ClipboardDataMan.GetTimeNoToSecond() + ".txt";
            TxtTool.Create(fileName, "");
            System.Diagnostics.Process.Start("notepad.exe", fileName);
        }

        /// <summary>
        /// 图片保存到本地
        /// </summary>
        /// <param name="img"></param>
        public static void SaveImg(Image img)
        {
            string fileName = R.Paths.MyDataPath + "\\screeen_short\\img";
            if (File.Exists(fileName))
            {
            }
            else
            {
                Directory.CreateDirectory(fileName);
            }
            fileName = fileName + "\\" + ClipboardDataMan.GetTimeNoToSecond() + ".jpg";
            img.Save(fileName);
        }
        #endregion

        #region 模拟Http GET 请求

        /// <summary>
        /// 模拟Http GET 请求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string HttpGet(string url)
        {
            String result = String.Empty;
            try
            {
                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
                wbRequest.Method = "GET";
                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    using (StreamReader sReader = new StreamReader(responseStream))
                    {
                        result = sReader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return result;
        }

        #endregion

        #region 开始任务/刷新界面/清空Txt日志


        /// <summary>
        /// 开始任务
        /// </summary>
        private void Start()
        {
            Task.Factory.StartNew(MyTask);
        }

        /// <summary>
        /// Http Get任务
        /// </summary>
        private void MyTask()
        {
            String url = Tb_net1.Text.ToString();
            String reponse = HttpGet(url);
            url_result = reponse;
            this.Invoke(_mydelegate);
        }

        /// <summary>
        /// 刷新界面
        /// </summary>
        private void RefreshView()
        {
            //日志消息
            try
            {
                Tb_response.AppendText("---" + DateTime.Now.ToString() + "---" + "\n");
                Tb_response.AppendText("---result---:" + "\n" + url_result + "\n");
            }
            catch (Exception e)
            {

            }

        }

        /// <summary>
        /// 清空Txt日志
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_reset_Click(object sender, EventArgs e)
        {
            Tb_response.Text = "";
        }



        #endregion

        
    }
}
